﻿using System;

namespace Galaktika.BI.Runtime.Services
{
    public class UserTaskDescriptor : ITaskDescriptor
    {
        public UserTaskDescriptor(string caption)
        {
            this.Caption = caption;
            this.Started = DateTime.Now;
        }

        public UserTaskDescriptor(string caption, object currentForm) : this(caption)
        {
            this.CurrentForm = currentForm;
        }

        public readonly string Caption;
        public readonly DateTime Started;
        public DateTime? Finished;
        public string FinishMessage;
        public Exception Exception;

        public WorkItemState CurrentState
        {
            get
            {
                if (this.Finished == null)
                {
                    return WorkItemState.Running;
                }

                return WorkItemState.Done;
            }
        }

        public override string ToString()
        {
            switch (this.CurrentState)
            {
                case WorkItemState.Running:
                    return string.Format("{0} {1}... {2}", 
                        this.Started.ToLongTimeString(), 
                        this.Caption,
                        this.CurrentState);
                case WorkItemState.Done:
                    return string.Format("{0} {1}... {2}",
                        this.Started.ToLongTimeString(),
                        this.Caption,
                        string.IsNullOrEmpty(this.FinishMessage) ? this.CurrentState.ToString() : this.FinishMessage);
            }
            return base.ToString();
        }

        #region ITaskDescriptor Members

        public object CurrentForm { get; set; }
        
        #endregion
    }

    public enum WorkItemState
    {
        Running,
        Done
    }
}
